package Messages;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Server.SQLConnection;
import ocsf.server.ConnectionToClient;

public class MessageAddScenario {
	
	private ConnectionToClient client ;
	private ArrayList<Object> str ; 
	public static String TableName;
	private ResultSet RunNewRs;
	static int row1;
	
public MessageAddScenario(ArrayList<Object> msg, ConnectionToClient client) {
		
		this.client= client ;
		this.str= msg; 		
		
	}

public void Run(String ScenarioName, String Version, String Project) throws SQLException, IOException {
	
	SQLConnection sc = new SQLConnection();
	
	PreparedStatement Bankps = sc.getConnection().prepareStatement("INSERT INTO sbank (idSBank,SName,Version,Project) VALUES(?,?,?,?);");
	PreparedStatement AllBank = sc.getConnection().prepareStatement("SELECT * FROM sbank;");
	
	ResultSet Allrs= AllBank.executeQuery();
	row1 = 0;
	while(Allrs.next()){
		int temp = Allrs.getInt(1);
			if(row1>temp)
				row1=row1;
				else{
				row1=temp;
				}
	
	}

	Bankps.setInt(1, this.row1+1);
	Bankps.setString(2,ScenarioName);
	Bankps.setString(3,Version);
	Bankps.setString(4,Project);
	Bankps.executeUpdate();
	Bankps.close();
	
	
	TableName=new StringBuilder().append(ScenarioName).append("_").append(Version).toString();
	// System.out.println(TableName);
	//PreparedStatement ScenarioNew = sc.getConnection().prepareStatement("create table "+TableName+" (idScenario int, TestDescription VARCHAR(400),Teststep VARCHAR(400),Expected VARCHAR(400),Actual VARCHAR(400),Status VARCHAR(45),Comment VARCHAR(400)  );");

	 PreparedStatement ScenarioNew1 = sc.getConnection().prepareStatement("create table "+ScenarioName+" as(SELECT * FROM hts.temp) ;");
	 PreparedStatement ScenarioKey = sc.getConnection().prepareStatement("ALTER TABLE "+ScenarioName+" ADD PRIMARY KEY (idScenario);");

	 		
	ScenarioNew1.executeUpdate();
	ScenarioKey.executeUpdate();
	ScenarioNew1.close();
	ScenarioKey.close();
	
	java.sql.PreparedStatement ScenarioTemp = sc.getConnection().prepareStatement("SELECT t.`idScenario`,t.`TestDescription`,t.`Status` FROM hts."+ScenarioName+" t;");

	RunNewRs= ScenarioTemp.executeQuery();
	ArrayList<Object> Arr=new ArrayList<Object>();
    Arr.add("AddScenario");
    

    Arr.add(ScenarioName);
    Arr.add("1");
	while (RunNewRs.next())
	{
		Arr.add(RunNewRs.getInt(1));
		Arr.add(RunNewRs.getString(2));
		
		Arr.add(RunNewRs.getString(3));
		

	}
	
   
    
	client.sendToClient(Arr);
	
	
	
	
	





	}//end of Run


		
	

}//End of class
